package fr.hoteia.appengine.archetype.server.web.servlet;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sourceforge.wurfl.core.Device;
import net.sourceforge.wurfl.core.WURFLManager;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;

public class ExtDispatcherServlet extends DispatcherServlet {

	/**
	 * Generated UID
	 */
	private static final long serialVersionUID = 2588762975032561784L;

	protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
		initPlatformType(request);
		super.doDispatch(request, response);
	}

	private void initPlatformType(HttpServletRequest request){
		
		ServletContext context = getServletContext();
		ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

		WURFLManager wurfl = (WURFLManager)ctx.getBean("wurflManager");
		Device device = wurfl.getDeviceForRequest(request);

        request.getSession().setAttribute("device", device);
	}
}
